﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ShoppingPlanner
{
    public partial class ShoppingPlanner : Form
    {
        shoppingPlannerDataContext myDB = new shoppingPlannerDataContext();
        public ShoppingPlanner()
        {
            InitializeComponent();
        }

        private void ShoppingPlanner_Load(object sender, EventArgs e)
        {
            loadInfo();
        }

        #region "Methods"

        #region "Load Data"
        private void loadInfo()
        {
            fillTypes();
        }

        private void fillTypes()
        {
            myDB = new shoppingPlannerDataContext();

            var types = from type in myDB.Types
                        select type;

            cmbType.DisplayMember = "typeName";
            cmbType.ValueMember = "typeID";
            cmbType.DataSource = types;


            var itemTypes = from itType in myDB.Types
                            select itType;

            cmbItemtype.DisplayMember = "typeName";
            cmbItemtype.ValueMember = "typeID";
            cmbItemtype.DataSource = itemTypes;

        }

        private void fillItems()
        {
            myDB = new shoppingPlannerDataContext();

            var items = from item in myDB.Items
                        where item.typeID == Convert.ToInt32(cmbType.SelectedValue)
                        select item;

            if (Enumerable.Count(items) > 0)
            {
                cmbItem.DisplayMember = "itemName";
                cmbItem.ValueMember = "itemID";
                cmbItem.DataSource = items;
                cmbItem.Enabled = true;
            }
            else
            { cmbItem.Enabled = false; }

        }

        #endregion "Load Data"

        #region "Save"

        private void saveItem()
        {
            try
            {
                myDB = new shoppingPlannerDataContext();

                // Adding Records – (1) Create a new object and sub-objects, 
                // (2) Add it to the DataContext collection, (3) Call SubmitChanges 

                // (1)
                Item item = new Item();
                item.ItemName = txtItem.Text;
                item.typeID = Convert.ToInt32(cmbItemtype.SelectedValue);

                // Create sub-record and add to this contact
                //Orders newOrder         = new Orders();
                //newOrder.Products       = (from p in db.Products 
                //where p.ProductName == "Asset Blaster Pro" 
                //select p).FirstOrDefault();
                //newOrder.DateOfPurchase = DateTime.Now;

                // (2)
                //newContact.Orders.Add(newOrder);
                myDB.Items.InsertOnSubmit(item);

                // (3)
                myDB.SubmitChanges();

                MessageBox.Show("Saved");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void saveType()
        {
            try
            {
                myDB = new shoppingPlannerDataContext();

                Type type = new Type();
                type.typeName = txtType.Text;

                myDB.Types.InsertOnSubmit(type);
                myDB.SubmitChanges();

                MessageBox.Show("Saved");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void saveShopping()
        {
            try
            {
                myDB = new shoppingPlannerDataContext();

                Shopping shopping = new Shopping();
                shopping.shoppingDate = DateTime.Now;
                shopping.typeID = Convert.ToInt32(cmbType.SelectedValue);
                if (cmbItem.Enabled)
                { shopping.itemID = Convert.ToInt32(cmbItem.SelectedValue); }
                shopping.description = txtDesc.Text;
                shopping.count = upDnCount.Value;
                try
                { shopping.totalPrice = Convert.ToDecimal(txtTotPrice.Text); }
                catch { shopping.totalPrice = 0; }

                myDB.Shoppings.InsertOnSubmit(shopping);
                myDB.SubmitChanges();

                MessageBox.Show("Saved");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        #endregion "Save"

        #endregion

        #region "Events"

        private void cmbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            fillItems();
        }

        private void btnSaveType_Click(object sender, EventArgs e)
        {
            saveType();
            fillTypes();
        }

        private void btnSaveItem_Click(object sender, EventArgs e)
        {
            saveItem();
            fillItems();
        }

        private void btnSaveShopping_Click(object sender, EventArgs e)
        {
            saveShopping();
        }

        #endregion

        private void tabPlanner_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabPlanner.SelectedTab == tabHistory)
            {
                myDB = new shoppingPlannerDataContext();

                List<Shopping> history = (from shopping in myDB.Shoppings select shopping).ToList();
                DataTable dt = new DataTable();


                dt.Columns.Add("Name");
                dt.Columns.Add("Type");
                dt.Columns.Add("Total Price");
                dt.Columns.Add("Count");
                dt.Columns.Add("Date");
                dt.Columns.Add("Description");


                foreach (Shopping shop in history)
                {
                    DataRow dr = dt.NewRow();
                    dr["Name"] = shop.Item == null ? "" : shop.Item.ItemName;
                    dr["Type"] = shop.Type.typeName;
                    dr["Total Price"] = shop.totalPrice;
                    dr["Count"] = shop.count;
                    dr["Date"] = shop.shoppingDate;
                    dr["Description"] = shop.description;
                    dt.Rows.Add(dr);
                }

                this.dgViewData.DataSource = dt;
            }
        }

        //Validation
        //Update
        //Delete
        //Reports
        

    }
}
